package cn.agiledata.bank.myspace.action;

import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.servlet.InitForString;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.PeerBankAccount;
import cn.agiledata.bank.myspace.bean.PeerBankAccountBean;
import cn.agiledata.bank.myspace.bean.PeerBankAccountQueryBean;
import cn.agiledata.bank.myspace.form.PeerBankAccountForm;
import cn.agiledata.bank.myspace.form.PeerBankAccountQueryForm;
import cn.agiledata.bank.myspace.service.PeerAccountService;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class PeerBankAccountAction extends IPBBaseAction implements Constant {
	/**
	 * 得到service
	 * @return
	 */
	public PeerAccountService getPeerAccountService() {
		return (PeerAccountService)IPBAppFactory.getObj("peerAccountService");
	}
	/**
	 * 得到银行账户列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getPeerBankAccountList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		PeerAccountService service = getPeerAccountService();
		PeerBankAccountQueryForm form = (PeerBankAccountQueryForm)actionForm;
		PeerBankAccountQueryBean bean = new PeerBankAccountQueryBean();
		bean.setAccountId(new Long(WeatherUtil.getAccountIdFromSession(request)));
		bean.setPageRecord(String.valueOf(Constant.PAGESIZE));
		if(StringUtil.isStrEmpty(form.getStartPage())) {
			bean.setStartPage("1");
		} else {
			bean.setStartPage(form.getStartPage());
		}
		PageBean pb = service.getPeerBankAccountList(bean);
		request.setAttribute("list", pb);
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 链接到添加银行账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward toPeerBankAccountAdd(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		if (isPersonAccount(request)) {
            // 加载个人收款银行列表
            request.setAttribute("bankList", InitForString.dataDictMap.get("BANK_INFO"));
        } else {
            // 加载企业收款银行列表
            request.setAttribute("bankList", InitForString.dataDictMap.get("ENTERPRISE_BANK_INFO"));
        }
        
		request.setAttribute("province", InitForString.dataDictMap.get("PROVINCE_INFO"));
		
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 添加银行账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public ActionForward peerBankAccountAdd(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		PeerBankAccountBean bean = new PeerBankAccountBean();
		BeanUtils.copyProperties(bean, actionForm);
		bean.setAccountId(getCurrentAccount(request).getId());
		
		PeerAccountService service = getPeerAccountService();
		service.peerBankAccountAdd(bean);
		
		//super.saveMessage(request,"peer_inner_account_add_success",null);
		//request.setAttribute("back",request.getContextPath()+mapping.findForwardConfig("back").getPath());
		//return mapping.findForward(FORWARD_GLOBAL_SUCCESS);
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 链接到查看银行账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getPeerBankAccount(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		
		PeerAccountService service = getPeerAccountService();
		PeerBankAccount p = (PeerBankAccount)service.getPeerBankAccount(Long.parseLong(id));
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		if (!p.getAccount().getId().equals(new Long(accountId))) p = null;
		request.setAttribute("peerBankAccount", p);
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 链接到修改银行账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward toPeerBankAccountModify(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		if (isPersonAccount(request)) {
            // 加载个人收款银行列表
            request.setAttribute("bankList", InitForString.dataDictMap.get("BANK_INFO"));
        } else {
            // 加载企业收款银行列表
            request.setAttribute("bankList", InitForString.dataDictMap.get("ENTERPRISE_BANK_INFO"));
        }
        
		request.setAttribute("province", InitForString.dataDictMap.get("PROVINCE_INFO"));
		
		String id = request.getParameter("id");
		
		PeerAccountService service = getPeerAccountService();
		PeerBankAccount p = (PeerBankAccount)service.getPeerBankAccount(Long.parseLong(id));
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		if (!p.getAccount().getId().equals(new Long(accountId))) p = null;
		
		request.setAttribute("id", String.valueOf(p.getId()));
		PeerBankAccountForm form = (PeerBankAccountForm)actionForm;
		BeanUtils.copyProperties(form, p);
		form.setBankSeq(String.valueOf(p.getBank().getId()));
		form.setProvSeq(String.valueOf(p.getProvince().getCityId()));
		request.setAttribute("city", InitForString.dataDictMap.get(form.getProvSeq()));
		form.setCitySeq(String.valueOf(p.getCity().getCityId()));
		
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 修改银行账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward peerBankAccountModify(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		PeerBankAccountBean bean = new PeerBankAccountBean();
		BeanUtils.copyProperties(bean, actionForm);
		bean.setAccountId(getCurrentAccount(request).getId());
		
		PeerAccountService service = getPeerAccountService();
		service.peerBankAccountModify(bean);
		
		//super.saveMessage(request,"peer_inner_account_modify_success",null);
		//request.setAttribute("back",request.getContextPath()+mapping.findForwardConfig("back").getPath());
		//return mapping.findForward(FORWARD_GLOBAL_SUCCESS);
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 删除银行账户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward peerBankAccountDelete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		
		PeerAccountService service = getPeerAccountService();
		service.peerBankAccountDelete(id);
		
		return mapping.findForward(SUCCESS);
	}
	
	/**
     * 获取当前账户
     */
    protected final Account getCurrentAccount(HttpServletRequest request) {
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        AccountTemplateInter accountTemplate = (AccountTemplateInter) IPBAppFactory.getObj(ForeDict.ACCOUNT_TEMPLATE);
        return accountTemplate.getAccount(String.valueOf(accountId));
    }
    
	/**
     * 判断当前用户是否个人用户
     */
    protected final boolean isPersonAccount(HttpServletRequest request) {
        return isPersonAccount(getCurrentAccount(request));
    }
    
    /**
     * 判断指定用户是否个人用户
     */
    protected final boolean isPersonAccount(Account account) {
        return Constant.ACCOUNT_TYPE_BUYER.equals(account.getAccountType());
    }
    
    /**
     * 新增银行联系人(在提交账户直付信息之前)
     */
    public ActionForward peerBankAccountAddBeforeDrawToOther(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IPBException,Exception {
    	// TODO Auto-generated method stub
    	PeerBankAccountBean bean = new PeerBankAccountBean();
    	parsePeerBankAccountBean(bean, request);
    	
    	PeerAccountService service = getPeerAccountService();
    	PrintWriter out = response.getWriter();
    	
    	int returnValue = service.peerBankAccountAddBeforeDrawToOther(bean);
    	
    	/*
    	if (returnValue==0) {
    		System.out.println("############# peerBankAccountAddBeforeDrawToOther success.");
    	} else if (returnValue==1) {
    		System.out.println("############# peerBankAccountAddBeforeDrawToOther failed(throw exception). ");
    	} else {
    		System.out.println("############# peerBankAccountAddBeforeDrawToOther failed(duplicated cardNo).");
    	}
    	*/
    	
    	// 成功输出0，异常输出1，卡号重复输出2
    	out.print(returnValue);
		return null;
    }
    
    /**
     * 把DrawToOtherForm中有关联系人的内容copy到PeerBankAccountBean里
     */
    protected final PeerBankAccountBean parsePeerBankAccountBean(PeerBankAccountBean bean, HttpServletRequest request) {

    	bean.setAccountId(getCurrentAccount(request).getId());
    	
    	bean.setPeerName(request.getParameter("creditName"));
    	bean.setBankSeq(request.getParameter("creditBankId"));
    	bean.setProvSeq(request.getParameter("creditProvinceId"));
    	bean.setCitySeq(request.getParameter("creditCityId"));
    	bean.setBankName(request.getParameter("creditBankName"));
    	bean.setCardNo(request.getParameter("creditAccountNo"));
    	
    	bean.setPeerEmail(request.getParameter("creditEmail"));
    	bean.setPeerPhone(request.getParameter("creditFixedTel"));
    	bean.setPeerMobile(request.getParameter("creditMobile"));
    	bean.setRemark(request.getParameter("remark"));
		
		return bean;
    }
    
}
